<template>
  <div class="file-manager">
    <!-- 右抽屉按钮 -->
     <!-- 顶部操作按钮 -->
        <div class="action-buttons">
          <el-button 
            class="back-btn" 
            type="primary" 
            :disabled="!DataList.folder.parentId"
            @click="handleBackClick"
          >
            <el-icon><ArrowLeft /></el-icon>
            返回上级
          </el-button>
          
		  <!-- 当前路径显示 -->
			<div class="current-path">
			  <el-icon><Location /></el-icon>
			  <span>{{ DataList.folder?.url }}</span>
			</div>
		  
          <el-button 
            class="drawer-btn" 
            type="primary" 
            @click="drawerVisible = true"
          >
            <el-icon><User /></el-icon>
            员工列表
          </el-button>
        </div>
    
    <!-- 文件夹列表 -->
    <div class="folder-section">
      <h3>文件夹</h3>
      <el-row :gutter="20">
        <el-col 
          v-for="folder in DataList.folderList" 
          :key="folder.id"
          :xs="12" :sm="8" :md="6" :lg="4" :xl="3"
        >
          <el-card 
            shadow="hover" 
            class="folder-card" 
            @dblclick="handleFolderDblClick(folder.id)"
            @click="handleFolderClick(folder)"
          >
            <div class="folder-icon">
              <el-icon :size="40"><Folder /></el-icon>
            </div>
            <div class="folder-name">
              {{ folder.name }}
            </div>
            <div class="folder-time">
              创建: {{ formatTime(folder.createTime) }}
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 文件列表 -->
    <div class="file-section">
      <h3>文件</h3>
      <el-table :data="DataList.fileList" style="width: 100%">
        <el-table-column prop="name" label="文件名" width="180">
          <template #default="{ row }">
            <el-icon><Document /></el-icon>
            <span style="margin-left: 10px">{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间">
          <template #default="{ row }">
            {{ formatTime(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="修改时间">
          <template #default="{ row }">
            {{ formatTime(row.updateTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button 
              type="primary" 
              size="small"
              @click="handleDownloadFile(row)"
            >
              下载
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <!-- 员工列表抽屉 -->
    <el-drawer
      v-model="drawerVisible"
      title="部门员工列表"
      direction="rtl"
      size="30%"
    >
      <el-collapse v-model="activeDept">
        <el-collapse-item 
          v-for="dept in depEmpList" 
          :key="dept.deptName"
          :title="dept.deptName"
          :name="dept.deptName"
        >
          <div 
            v-for="emp in dept.empList" 
            :key="emp.eid"
            class="emp-item"
            @dblclick="handleEmpDblClick(emp.eid)"
          >
            <el-icon><User /></el-icon>
            <span>{{ emp.ename }}</span>
            <el-tag size="small" type="info">{{ emp.eid }}</el-tag>
            <el-tag size="small" :type="emp.esex === 1 ? 'primary' : 'danger'">
              {{ emp.esex === 1 ? '男' : '女' }}
            </el-tag>
          </div>
        </el-collapse-item>
      </el-collapse>
    </el-drawer>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { 
  Document,
  Folder,
  User,
  ArrowLeft,
  Location
} from '@element-plus/icons-vue'
import req from '@/request';

// 抽屉控制
const drawerVisible = ref(false)
const activeDept = ref([])

// 初始化数据结构
const DataList = ref({
  folder: {},
  folderList: [],
  fileList: []
})

// 返回处理方法
const handleBackClick = () => {
  if (DataList.value.folder?.parentId) {
    content(DataList.value.folder.parentId)
  }
}

// 部门员工信息
const depEmpList = ref([])
const getDepEmpList = async () => {
  const res = await req.get('/info/emp/dept/detail')
  depEmpList.value = res.data
  // 默认展开第一个部门
  if (depEmpList.value.length > 0) {
    activeDept.value = [depEmpList.value[0].deptName]
    // 获取第一个员工的eid并加载内容
    const firstEmpEid = depEmpList.value[0]?.empList[0]?.eid
    if (firstEmpEid) {
      content(firstEmpEid)
    }
  }
}

// 双击员工处理
const handleEmpDblClick = async (eid) => {
  try {
    const res = await req.get(`/folder/select/${eid}`)
    DataList.value = res.data
    drawerVisible.value = false // 可选：操作后关闭抽屉
    console.log("获取员工文件夹成功:", res.data)
  } catch (error) {
    console.error("请求出错:", error)
  }
}

// 当前选中的文件夹
const selectedFolder = ref(null)

// 格式化时间显示
const formatTime = (timeArray) => {
  if (!timeArray) return '-'
  return `${timeArray[0]}-${timeArray[1]}-${timeArray[2]} ${timeArray[3]}:${timeArray[4]}:${timeArray[5]}`
}

// 获取文件夹内容方法
const content = async (eid) => {
  try {
    const res = await req.get(`/folder/select/${eid}`)
    DataList.value = res.data
    console.log("获取数据成功:", res.data)
  } catch (error) {
    console.error("请求出错:", error)
  }
}

// 双击文件夹处理
const handleFolderDblClick = (folderId) => {
  content(folderId)
}

// 单击文件夹处理（可选，用于选中效果）
const handleFolderClick = (folder) => {
  selectedFolder.value = folder
}

// 文件下载方法
const handleDownloadFile = (file) => {
  // 必须由用户点击事件直接触发
  const link = document.createElement('a');
  link.href = `/file/download?fileId=${file.id}`;
  link.download = file.name; 
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
};

// 初始化加载
onMounted(() => {
  getDepEmpList()
})
</script>

<style scoped>
	
.action-buttons {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.back-btn {
  margin-right: 10px;
}

/* 原有样式保持不变 */
.file-manager {
  padding: 20px;
}

.folder-section, .file-section {
  margin-bottom: 30px;
}

.folder-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s;
  user-select: none; /* 防止双击选中文本 */
}

.folder-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.1);
}

/* 选中效果 */
.folder-card.selected {
  border: 1px solid #409eff;
  background-color: #f5f9ff;
}

.folder-icon {
  text-align: center;
  color: #409eff;
  margin-bottom: 10px;
}

.folder-name {
  text-align: center;
  font-weight: bold;
  margin-bottom: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.folder-time {
  font-size: 12px;
  color: #999;
  text-align: center;
}

.emp-item {
  padding: 8px 0;
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer; /* 添加这一行 */
}

.emp-item:hover {
  background-color: #f5f7fa;
}
/* 导航样式 */
.action-buttons {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}

.current-path {
  flex: 1;
  margin: 0 20px;
  padding: 0 15px;
  background-color: #f5f7fa;
  border-radius: 4px;
  height: 32px;
  line-height: 32px;
  display: flex;
  align-items: center;
  color: #666;
}

.current-path .el-icon {
  margin-right: 8px;
  color: var(--el-color-primary);
}
</style>